// Package sys
// @Link  https://github.com/bufanyun/hotgo
// @Copyright  Copyright (c) 2024 HotGo CLI
// @Author  Ms <133814250@qq.com>
// @License  https://github.com/bufanyun/hotgo/blob/master/LICENSE
// @AutoGenerate Version 2.13.1
package sys

import (
	"context"
	"hotgo/api/admin/stocklive"
	"hotgo/internal/model/input/form"
	"hotgo/internal/model/input/sysin"
	"hotgo/internal/service"
	"strings"
)

var (
	StockLive = cStockLive{}
)

type cStockLive struct{}

// List 查看大盘直播列表
func (c *cStockLive) List(ctx context.Context, req *stocklive.ListReq) (res *stocklive.ListRes, err error) {
	list, totalCount, err := service.SysStockLive().List(ctx, &req.StockLiveListInp)
	if err != nil {
		return
	}

	if list == nil {
		list = []*sysin.StockLiveListModel{}
	}

	for _, model := range list {
		stockCodes := strings.Split(model.Stocks, ",")
		var spotEmIn = &sysin.StockZhASpotEmListInp{
			PageReq: form.PageReq{PerPage: 200},
			Codes:   stockCodes,
		}
		stocks, _, err := service.SysStockZhASpotEm().List(ctx, spotEmIn)
		if err != nil {
			continue
		}
		for _, stock := range stocks {
			model.StockItems = append(model.StockItems, sysin.StockItem{
				Name:          stock.Name,
				ChangePercent: stock.ChangePercent,
			})
		}
	}
	res = new(stocklive.ListRes)
	res.List = list
	res.PageRes.Pack(req, totalCount)
	return
}

// Export 导出大盘直播列表
func (c *cStockLive) Export(ctx context.Context, req *stocklive.ExportReq) (res *stocklive.ExportRes, err error) {
	err = service.SysStockLive().Export(ctx, &req.StockLiveListInp)
	return
}

// Edit 更新大盘直播
func (c *cStockLive) Edit(ctx context.Context, req *stocklive.EditReq) (res *stocklive.EditRes, err error) {
	err = service.SysStockLive().Edit(ctx, &req.StockLiveEditInp)
	return
}

// View 获取指定大盘直播信息
func (c *cStockLive) View(ctx context.Context, req *stocklive.ViewReq) (res *stocklive.ViewRes, err error) {
	data, err := service.SysStockLive().View(ctx, &req.StockLiveViewInp)
	if err != nil {
		return
	}

	res = new(stocklive.ViewRes)
	res.StockLiveViewModel = data
	return
}

// Delete 删除大盘直播
func (c *cStockLive) Delete(ctx context.Context, req *stocklive.DeleteReq) (res *stocklive.DeleteRes, err error) {
	err = service.SysStockLive().Delete(ctx, &req.StockLiveDeleteInp)
	return
}
